package com.huluobo.a2112mvvmdouyindemo.setting;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.bean.SettingBeanResult;
import com.huluobo.mvvm_base.bean.UserBeanResult;
import com.huluobo.mvvm_base.net.Const;
import com.huluobo.mvvm_base.viewmodel.BaseViewModel;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/4/12.
 */
public class SettingViewModel extends BaseViewModel<SettingRepo> {

    MutableLiveData<SettingBeanResult> updateIconSuccess = new MutableLiveData<>();

    MutableLiveData<String> updateIconFailed = new MutableLiveData<>();

    MutableLiveData<UserBeanResult.UserBean> getUserSuccess = new MutableLiveData<>();

    MutableLiveData<String> getUserFailed = new MutableLiveData<>();

    MutableLiveData<SettingBeanResult> updateAddressSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateAddressFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateNicknameSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateNicknameFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateBirthSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateBirthFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateSexSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateSexFailed = new MutableLiveData<>();

    public void updateAddress(String address) {
        repo.updateAddress(address, updateAddressSuccess, updateAddressFailed);
    }

    public void updateNickname(String nickname) {
        repo.updateNickname(nickname, updateNicknameSuccess, updateNicknameFailed);
    }

    public void updateBirth(String birth) {
        repo.updateBrith(birth, updateBirthSuccess, updateBirthFailed);
    }

    public void updateSex(String sex) {
        repo.updateSex(sex, updateSexSuccess, updateSexFailed);
    }

    public void getUserByName(String username) {
        repo.queryUserInfoByName(username, getUserSuccess, getUserFailed);
    }

    public void updateUserIcon(String filePath) {
        //1.创建文件对象
        File file = new File(filePath);
        //2.封装body
        RequestBody requestBody = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), file);
        //3.封装成Part
        //第一个参数是解耦文档中的参数key,第二个参数是文件名,第三个参数是生成的body
        MultipartBody.Part part = MultipartBody.Part.createFormData(Const.PARAM_FILE, file.getName(), requestBody);
        repo.updateIcon(part, updateIconSuccess, updateIconFailed);
    }

    @Override
    protected SettingRepo createRepo() {
        return new SettingRepo();
    }
}
